// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package cn.dev8.sdk.kotlin

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.ui.Messages
import nbcp.base.extend.basicSame
import org.slf4j.LoggerFactory
import java.io.File

class JavaDocSwaggerAction1 : SwaggerAction() {
    companion object {
        private val logger = LoggerFactory.getLogger(javaClass.declaringClass);
    }


    override fun update(e: AnActionEvent) {
        val project = e.getData(PlatformDataKeys.PROJECT)
        val editor = e.getData(PlatformDataKeys.EDITOR)
        val file = e.getData(PlatformDataKeys.VIRTUAL_FILE)
        e.presentation.isEnabled = project != null && editor != null &&  file != null && file.extension basicSame "java";
    }

    override fun actionPerformed(event: AnActionEvent) {
        val project = event.getData(PlatformDataKeys.PROJECT)

        if (project == null || !project.isOpen) return;
        var file = event.getData(PlatformDataKeys.VIRTUAL_FILE);
        if( file == null){
            return;
        }


        logger.warn(file.path)

        var count = walk(File(file.path));

        if (count == 0) {
            Messages.showMessageDialog(
                project,
                "该插件会根据 java doc 生成 Swagger 注解，在项目中没有发现要生成的Swagger注解!",
                "生成Swagger注解",
                Messages.getInformationIcon()
            )
        } else {
            Messages.showMessageDialog(
                project,
                "生成Swagger注解 完成. 处理了 ${count} 个文件, 请格式化后提交!",
                "生成Swagger注解",
                Messages.getInformationIcon()
            )
        }
    }

}
